/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FamiliaresForm.java
 *
 * Created on Jun 24, 2011, 6:03:36 PM
 */

package mutual;

import java.awt.Dialog;
import javax.persistence.EntityManager;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 *
 * @author yue
 */
public class FamiliaresForm extends javax.swing.JPanel {

    EntityManager entityManager;

    public FamiliaresForm(EntityManager em){
        entityManager = em;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mutual.MutualApp.class).getContext().getResourceMap(FamiliaresForm.class);
        tipoDocumentoQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("tipoDocumentoQuery.query")); // NOI18N
        parentescoQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("parentescoQuery.query")); // NOI18N
        nivelEducacionQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("nivelEducacionQuery.query")); // NOI18N
        tipoDocumentoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tipoDocumentoQuery.getResultList();
        parentescoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : parentescoQuery.getResultList();
        nivelEducacionList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : nivelEducacionQuery.getResultList();
        familiaresTitleLabel = new javax.swing.JLabel();
        tipoDocumentoLabel = new javax.swing.JLabel();
        apellidoLabel = new javax.swing.JLabel();
        nombreLabel = new javax.swing.JLabel();
        numeroDocumentoLabel = new javax.swing.JLabel();
        parentescoLabel = new javax.swing.JLabel();
        fechaNacimientoLabel = new javax.swing.JLabel();
        nivelEducacionLabel = new javax.swing.JLabel();
        bloqueadoLabel = new javax.swing.JLabel();
        observacionesLabel = new javax.swing.JLabel();
        fechaNacimientoField = new org.jdesktop.swingx.JXDatePicker();
        nivelEducacionField = new javax.swing.JComboBox();
        parentescoField = new javax.swing.JComboBox();
        tipoDocumentoField = new javax.swing.JComboBox();
        apellidoField = new javax.swing.JTextField();
        nombreField = new javax.swing.JTextField();
        bloqueadoField = new javax.swing.JCheckBox();
        observacionesField = new javax.swing.JTextField();
        numeroDocumentoField = new javax.swing.JTextField();
        addTipoDocumentoButton = new javax.swing.JButton();
        addParentescoButton = new javax.swing.JButton();
        addNivelEducacionButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        familiaresTitleLabel.setText(resourceMap.getString("familiaresTitleLabel.text")); // NOI18N
        familiaresTitleLabel.setName("familiaresTitleLabel"); // NOI18N

        tipoDocumentoLabel.setText(resourceMap.getString("tipoDocumentoLabel.text")); // NOI18N
        tipoDocumentoLabel.setName("tipoDocumentoLabel"); // NOI18N

        apellidoLabel.setText(resourceMap.getString("apellidoLabel.text")); // NOI18N
        apellidoLabel.setName("apellidoLabel"); // NOI18N

        nombreLabel.setText(resourceMap.getString("nombreLabel.text")); // NOI18N
        nombreLabel.setName("nombreLabel"); // NOI18N

        numeroDocumentoLabel.setText(resourceMap.getString("numeroDocumentoLabel.text")); // NOI18N
        numeroDocumentoLabel.setName("numeroDocumentoLabel"); // NOI18N

        parentescoLabel.setText(resourceMap.getString("parentescoLabel.text")); // NOI18N
        parentescoLabel.setName("parentescoLabel"); // NOI18N

        fechaNacimientoLabel.setText(resourceMap.getString("fechaNacimientoLabel.text")); // NOI18N
        fechaNacimientoLabel.setName("fechaNacimientoLabel"); // NOI18N

        nivelEducacionLabel.setText(resourceMap.getString("nivelEducacionLabel.text")); // NOI18N
        nivelEducacionLabel.setName("nivelEducacionLabel"); // NOI18N

        bloqueadoLabel.setText(resourceMap.getString("bloqueadoLabel.text")); // NOI18N
        bloqueadoLabel.setName("bloqueadoLabel"); // NOI18N

        observacionesLabel.setText(resourceMap.getString("observacionesLabel.text")); // NOI18N
        observacionesLabel.setName("observacionesLabel"); // NOI18N

        fechaNacimientoField.setFormats("dd/MM/yyyy");
        fechaNacimientoField.setName("fechaNacimientoField"); // NOI18N

        nivelEducacionField.setName("nivelEducacionField"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, nivelEducacionList, nivelEducacionField);
        bindingGroup.addBinding(jComboBoxBinding);

        parentescoField.setName("parentescoField"); // NOI18N

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, parentescoList, parentescoField);
        bindingGroup.addBinding(jComboBoxBinding);

        tipoDocumentoField.setName("tipoDocumentoField"); // NOI18N

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tipoDocumentoList, tipoDocumentoField);
        bindingGroup.addBinding(jComboBoxBinding);

        apellidoField.setName("apellidoField"); // NOI18N

        nombreField.setName("nombreField"); // NOI18N

        bloqueadoField.setName("bloqueadoField"); // NOI18N

        observacionesField.setName("observacionesField"); // NOI18N

        numeroDocumentoField.setName("numeroDocumentoField"); // NOI18N

        addTipoDocumentoButton.setText(resourceMap.getString("addTipoDocumentoButton.text")); // NOI18N
        addTipoDocumentoButton.setName("addTipoDocumentoButton"); // NOI18N
        addTipoDocumentoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTipoDocumentoButtonActionPerformed(evt);
            }
        });

        addParentescoButton.setText(resourceMap.getString("addParentescoButton.text")); // NOI18N
        addParentescoButton.setName("addParentescoButton"); // NOI18N
        addParentescoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addParentescoButtonActionPerformed(evt);
            }
        });

        addNivelEducacionButton.setText(resourceMap.getString("addNivelEducacionButton.text")); // NOI18N
        addNivelEducacionButton.setName("addNivelEducacionButton"); // NOI18N
        addNivelEducacionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNivelEducacionButtonActionPerformed(evt);
            }
        });

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 759, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(familiaresTitleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 661, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nombreLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 679, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(fechaNacimientoLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nivelEducacionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(parentescoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tipoDocumentoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(apellidoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bloqueadoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(observacionesLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bloqueadoField)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 560, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(parentescoField, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addParentescoButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(tipoDocumentoField, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(addTipoDocumentoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numeroDocumentoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numeroDocumentoField, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(nivelEducacionField, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addNivelEducacionButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fechaNacimientoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 441, Short.MAX_VALUE))
                            .addComponent(apellidoField, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                            .addComponent(nombreField, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                            .addComponent(observacionesField, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 679, Short.MAX_VALUE)
                        .addComponent(saveButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 407, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(familiaresTitleLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tipoDocumentoLabel)
                    .addComponent(tipoDocumentoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(numeroDocumentoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(numeroDocumentoLabel)
                    .addComponent(addTipoDocumentoButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apellidoLabel)
                    .addComponent(apellidoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreLabel)
                    .addComponent(nombreField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parentescoLabel)
                    .addComponent(parentescoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addParentescoButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fechaNacimientoLabel)
                    .addComponent(fechaNacimientoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nivelEducacionLabel)
                    .addComponent(nivelEducacionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addNivelEducacionButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bloqueadoLabel)
                    .addComponent(bloqueadoField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(observacionesLabel)
                    .addComponent(observacionesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void addTipoDocumentoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTipoDocumentoButtonActionPerformed
        new TipoDocumentoForm(entityManager).showDialog(frame);
        refreshQuery(tipoDocumentoQuery, tipoDocumentoList, tipoDocumentoField);
}//GEN-LAST:event_addTipoDocumentoButtonActionPerformed

    private void addParentescoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addParentescoButtonActionPerformed
        new ParentescosForm(entityManager).showDialog(frame);
        refreshQuery(parentescoQuery, parentescoList, parentescoField);
}//GEN-LAST:event_addParentescoButtonActionPerformed

    private void addNivelEducacionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNivelEducacionButtonActionPerformed
        new NivelEducacionForm(entityManager).showDialog(frame);
        refreshQuery(nivelEducacionQuery, nivelEducacionList, nivelEducacionField);
}//GEN-LAST:event_addNivelEducacionButtonActionPerformed

    private void refreshQuery(javax.persistence.Query query, java.util.List list, JComboBox cb){
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
        cb.updateUI();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNivelEducacionButton;
    private javax.swing.JButton addParentescoButton;
    private javax.swing.JButton addTipoDocumentoButton;
    private javax.swing.JTextField apellidoField;
    private javax.swing.JLabel apellidoLabel;
    private javax.swing.JCheckBox bloqueadoField;
    private javax.swing.JLabel bloqueadoLabel;
    private javax.swing.JLabel familiaresTitleLabel;
    private org.jdesktop.swingx.JXDatePicker fechaNacimientoField;
    private javax.swing.JLabel fechaNacimientoLabel;
    private javax.swing.JComboBox nivelEducacionField;
    private javax.swing.JLabel nivelEducacionLabel;
    private java.util.List nivelEducacionList;
    private javax.persistence.Query nivelEducacionQuery;
    private javax.swing.JTextField nombreField;
    private javax.swing.JLabel nombreLabel;
    private javax.swing.JTextField numeroDocumentoField;
    private javax.swing.JLabel numeroDocumentoLabel;
    private javax.swing.JTextField observacionesField;
    private javax.swing.JLabel observacionesLabel;
    private javax.swing.JComboBox parentescoField;
    private javax.swing.JLabel parentescoLabel;
    private java.util.List parentescoList;
    private javax.persistence.Query parentescoQuery;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox tipoDocumentoField;
    private javax.swing.JLabel tipoDocumentoLabel;
    private java.util.List tipoDocumentoList;
    private javax.persistence.Query tipoDocumentoQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    static JFrame frame;

    public void showDialog(JFrame parent){
        JDialog dialog = new JDialog(parent, "Agregar familiares", Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setContentPane(this);
        dialog.setSize(759,407);
        dialog.setVisible(true);
    }
}
